package com.wx.mall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.vo.user.UserReqVo;
import com.wx.mall.feign.OperateFeignClient;
import com.wx.mall.user.dto.DisUseDto;
import com.wx.mall.user.entity.User;
import com.wx.mall.user.mapper.UserMapper;
import com.wx.mall.user.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.user.vo.DisUseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-03-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private OperateFeignClient client;

    @Override
    public User getListById(Long userId) {

        User list=this.getById(userId);
        return list;
    };

    @Override
    public List<User> getListByOder(Integer pageNum, Integer pageSize,int sort) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<User> wrapper=new QueryWrapper<>();
//        0为会员等级，1为消费金额，2为有效订单数量s
        if(sort==0){
            wrapper.orderBy(true,true,"user_ms_id");
        };
        if(sort==1){
            wrapper.orderBy(true,true,"user_consume");
        };
        if(sort==2){
            wrapper.orderBy(true,true,"user_oder_number");
        }

        List<User> list=this.list(wrapper);
        return list;
    };

    @Override
    public List<User> getListByUser(Integer pageNum, Integer pageSize, Long userNumber,
                                    String userName, String userSource,
                                    Integer userMsId, String userJob,
                                    Date userCreateTime, int sort) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<User> wrapper=new QueryWrapper<>();
//        用户账号
        if(userNumber!=null){
            wrapper.eq("user_number",userNumber);
        };
//       用户昵称
        if(userName!=null){
            wrapper.eq("user_name",userName);
        };
//        用户来源
        if(userSource!=null){
            wrapper.eq("user_source",userSource);
        };
//        会员等级
        if(userMsId!=null){
            wrapper.eq("user_ms_id",userMsId);
        };
//用户职业
        if(userJob!=null){
            wrapper.eq("user_job",userJob);
        };
//注册时间
        if(userCreateTime!=null){
            wrapper.eq("user_create_time",userCreateTime);
        };
//排序
        //        0为会员等级，1为消费金额，2为有效订单数量s
        if(sort==0){
            wrapper.orderBy(true,true,"user_ms_id");
        };
        if(sort==1){
            wrapper.orderBy(true,true,"user_consume");
        };
        if(sort==2){
            wrapper.orderBy(true,true,"user_oder_number");
        }
        List<User> list=this.list(wrapper);
        return list;
    };

    @Override
    public int setListByIdList(UserReqVo vo) {
        List<User> userList=new ArrayList<>();
        for (Long id:vo.getIds()
        ) {
            User user=new User();
            user.setUserId(id);
            userList.add(user);
        }
        boolean sucees=false;
        if (vo.getType()==0){
            for (User user:userList
            ) {
                user.setUserIsActive(0);
            };
            sucees=this.updateBatchById(userList);
        };
        if(vo.getType()==1){
            for (User user:userList
            ) {
                user.setUserIsActive(1);
            };
            sucees=this.updateBatchById(userList);
        };
//        删除
        if (vo.getType()==2){
            for (User user:userList
            ) {
                sucees=this.removeById(user);
            }
        }



        return sucees==true?1:0;
    };


    @Override
    public CommonPage<DisUseVo> getTotal(DisUseDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

        QueryWrapper<User> wrapper=new QueryWrapper<>();

        if(dto.getUserId()!=null){

            wrapper.eq("user_id",dto.getUserId());
        }
        if(!dto.getUserName().equals("")){

            wrapper.like("user_name",dto.getUserName());
        }

        if(dto.getUserMsId()!=null){
            wrapper.eq("user_ms_id",dto.getUserMsId());
        }
        switch(dto.getSort()){
            case 0:
                wrapper.orderByAsc("user_name");
                break;
            case 1:
                wrapper.orderByAsc("user_ms_id");
                break;
        }
//获取到了use数据
        CommonPage<User> list= CommonPage.restPage(this.list(wrapper))  ;

        List<DisUseVo> useVoList=new ArrayList<>();
//数据改造
        for (User user:list.getList()
        ) {
//            转化
            DisUseVo vo=parseUser(user);
            useVoList.add(vo);

        }
        CommonPage<DisUseVo> pagelist=CommonPage.restPage(useVoList);
        pagelist.setTotalPage(list.getTotalPage());
        pagelist.setTotal(list.getTotal());
        pagelist.setPageSize(list.getPageSize());
        pagelist.setPageNum(list.getPageNum());
        return pagelist;
    };

    public DisUseVo parseUser(User user){

        DisUseVo vo=new DisUseVo();
//        复制
        BeanUtils.copyProperties(user,vo);

//        根据id获取记录

        Map<String,Integer> total=client.getTotal(user.getUserId());

        vo.setNotState(total.get("notState"));
        vo.setUseState(total.get("useState"));
        vo.setTimeOut(total.get("timeOut"));

        return vo;

    };


    @Override
    public DisUseVo getListByIdTwo(Long userId) {

        DisUseVo vo=new DisUseVo();
        User list=this.getById(userId);
        BeanUtils.copyProperties(list,vo);
        //        根据id获取记录

        Map<String,Integer> total=client.getTotal(userId);

        vo.setNotState(total.get("notState"));
        vo.setUseState(total.get("useState"));
        vo.setTimeOut(total.get("timeOut"));

        return vo;
    }
}
